package com.hzhc.controller.yworder;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.UserMemberService;
import com.hzhc.service.YwOrderService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.TrainPlanDetailVo;
import com.hzhc.vo.YwOrderCarVo;
import com.hzhc.vo.YwOrderVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 业务委托
 *
 * @author 功不唐捐
 * @since 2024-06-13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ywOrder")
public class YwOrderController extends BaseController {
    private final YwOrderService ywOrderService;

    private final UserMemberService userMemberService;

    @Value("${file.ywOderFileUploadFolder}")
    private String ywOderFileUploadFolder;
    @Value("${file.ywOderFileAccessPath}")
    private String ywOderFileAccessPath;

    @Value("${cms.targetUrl}")
    private String targetUrl;
    /**
     * 查询
     */
    @RequestMapping("/getOrderList")
    public ResultUtil<List<YwOrderVo>> getOrderList(@RequestBody YwOrderVo vo) {
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderVo> vos = ywOrderService.getOrderPage(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 新增()
     */
    @PostMapping("/addOrder")
    @SysTemLog(operModul = "商务受理-新增客户委托",operType = "新增",operDesc = "新增客户委托",modular="PC")
    public ResultUtil<T> addOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUserMember(request);
        if(user != null){
            vo.setMemberId(user.getId());
            vo.setUserName(user.getRealname());
        }
        vo.setCreateTime(LocalDateTime.now());
        vo.setAccessPath("YZS");
        return ywOrderService.addOrder(vo,targetUrl);
    }

    /**
     * 编辑
     */
    @PostMapping("/updateOrder")
    @SysTemLog(operModul = "商务受理-修改客户委托",operType = "修改",operDesc = "修改客户委托",modular="PC")
    public ResultUtil<T> updateOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUserMember(request);
        if(user != null){
            vo.setUpdateMemberId(user.getId());
            vo.setUpdateUserName(user.getRealname());
        }
        vo.setUpdateTime(LocalDateTime.now());
        return ywOrderService.updateOrder(vo);
    }

    /**
     * 提交
     */
    @PostMapping("/submitOrder")
    @SysTemLog(operModul = "商务受理-客户委托提交",operType = "提交",operDesc = "客户委托提交",modular="PC")
    public ResultUtil<T> submitOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUserMember(request);
        vo.setUpdateUserId(user.getId());
        vo.setUpdateUserName(user.getRealname());
        vo.setUpdateTime(LocalDateTime.now());
        return ywOrderService.submitOrder(vo);
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteOrder/{id}")
    @SysTemLog(operModul = "商务受理-删除客户委托",operType = "删除",operDesc = "删除客户委托",modular="PC")
    public ResultUtil<T> deleteOrder(@PathVariable("id") Integer id) {
        return ywOrderService.deleteOrder(id);
    }


//    /**
//     * 新增提空重进数据
//     */
//    @PostMapping("/addTkzjOrder")
//    @SysTemLog(operModul = "商务受理-新增客户委托",operType = "新增",operDesc = "新增客户委托",modular="PC")
//    public ResultUtil<T> addTkzjOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
//        UserDto user = TokenUtil.getUserMember(request);
//        vo.setUserId(user.getId());
//        vo.setUserName(user.getRealname());
//        vo.setCreateTime(LocalDateTime.now());
//        return ywOrderService.addTkzjOrder(request,vo);
//    }
//
//    /**
//     * 编辑提空重进数据
//     */
//    @PostMapping("/updateTkzjOrder")
//    @SysTemLog(operModul = "商务受理-修改客户委托",operType = "修改",operDesc = "修改客户委托",modular="PC")
//    public ResultUtil<T> updateTkzjOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
//        UserDto user = TokenUtil.getUserMember(request);
//        vo.setUpdateUserId(user.getId());
//        vo.setUpdateUserName(user.getRealname());
//        vo.setUpdateTime(LocalDateTime.now());
//        return ywOrderService.updateTkzjOrder(vo);
//    }

//    /**
//     * 新增提重委托
//     */
//    @PostMapping("/addTzOrder")
//    @SysTemLog(operModul = "商务受理-新增客户委托",operType = "新增",operDesc = "新增客户委托",modular="PC")
//    public ResultUtil<T> addTzOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
//        UserDto user = TokenUtil.getUserMember(request);
//        vo.setUserId(user.getId());
//        vo.setUserName(user.getRealname());
//        vo.setCreateTime(LocalDateTime.now());
//        return ywOrderService.addTzOrder(vo);
//    }
//
//    /**
//     * 编辑提重委托
//     */
//    @PostMapping("/updateTzOrder")
//    @SysTemLog(operModul = "商务受理-修改客户委托",operType = "修改",operDesc = "修改客户委托",modular="PC")
//    public ResultUtil<T> updateTzOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
//        UserDto user = TokenUtil.getUserMember(request);
//        vo.setUpdateUserId(user.getId());
//        vo.setUpdateUserName(user.getRealname());
//        vo.setUpdateTime(LocalDateTime.now());
//        return ywOrderService.updateTzOrder(vo);
//    }

    /**
     * yzs 散改集业务新增
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/addSgjOrder")
    @SysTemLog(operModul = "商务管理-新增散改集业务",operType = "新增",operDesc = "新增散改集业务",modular="PC")
    public ResultUtil<T> addSgjOrder(HttpServletRequest request,@RequestBody YwOrderVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        vo.setUserName(userMemberService.getById(userID).getRealName());
        vo.setAccessPath("YZS");
        return ywOrderService.addSgjOrder(vo,targetUrl);
    }

    /**
     * yzs 散改集列表list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getAllSgjList")
    public ResultUtil<List<YwOrderVo>> getAllSgjList(HttpServletRequest request,@RequestBody YwOrderVo vo){
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<YwOrderVo> list = ywOrderService.getAllSgjList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * yzs 散改集 修改
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/updateSgjOrder")
    @SysTemLog(operModul = "商务管理-修改散改集业务",operType = "修改",operDesc = "修改散改集业务",modular="PC")
    public ResultUtil<T> updateSgjOrder(HttpServletRequest request,@RequestBody YwOrderVo vo){
        UserDto user = TokenUtil.getUser(request);
        vo.setUpdateUserId(user.getId());
        vo.setUpdateUserName(user.getRealname());
        vo.setUpdateTime(LocalDateTime.now());
        return ywOrderService.updateSgjOrder(vo);
    }

    /**
     * yzs 散改集 删除
     * @param id
     * @return
     */
    @GetMapping("/deleteSgjOrder/{id}")
    @SysTemLog(operModul = "商务管理-删除散改集业务",operType = "删除",operDesc = "删除散改集业务",modular="PC")
    public ResultUtil<T> deleteSgjOrder(@PathVariable Long id){
        return ywOrderService.deleteSgjOrder(id);
    }


    /**
     * 水运单导入
     * @return
     */
    @RequestMapping("/importYwOrderFile")
    public ResultUtil<List<TrainPlanDetailVo>> importYwOrderFile(YwOrderVo vo,@RequestParam(value = "fileRef") MultipartFile fileRef){
        vo.setFile(fileRef);
        return ywOrderService.importYwOrderFile(vo,ywOderFileUploadFolder,ywOderFileAccessPath);
    }

    /**
     * 删除水运表
     */
    @RequestMapping("/delYwOrderFile")
    public ResultUtil<T> deleteYwOrderFile(@RequestBody YwOrderVo vo) {
        return ywOrderService.deleteYwOrderFile(vo.getId().intValue());
    }

    /**
     * 查询
     */
    @RequestMapping("/getPowerOrderList")
    public ResultUtil<List<YwOrderVo>> getPowerOrderList(@RequestBody YwOrderVo vo) {
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderVo> vos = ywOrderService.getPowerOrderList(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * yzs 授权车辆
     */
    @RequestMapping("/addPowerCar")
    public ResultUtil<T> addPowerCar(HttpServletRequest request,@RequestBody YwOrderVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        vo.setUserName(userMemberService.getById(userID).getRealName());
        return ywOrderService.addPowerCar(vo);
    }

    /**
     * 查询
     */
    @RequestMapping("/getPowerCarList")
    public ResultUtil<List<YwOrderCarVo>> getPowerCarList(@RequestBody YwOrderVo vo) {
        Page<YwOrderCarVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderCarVo> vos = ywOrderService.getPowerCarList(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }
}
